/*
Run Rollup in watch mode for development.

To specific the package to watch, simply pass its name and the desired build
formats to watch (defaults to "global"):

```
# name supports fuzzy match. will watch all packages with name containing "dom"
yarn dev dom

# specify the format to output
yarn dev core --formats cjs

# Can also drop all __DEV__ blocks with:
__DEV__=false yarn dev
```
*/

const execa = require('execa')
const { fuzzyMatchTarget } = require('./utils')


// args 解析 package.json 相关命令后面的参数
// 例如 dev 后面的  --sourcemap
const args = require('minimist')(process.argv.slice(2))

// 最终输入的模块 
// 对应 package.json 不带 _ 的参数
const target = args._.length ? fuzzyMatchTarget(args._)[0] : 'vue'

// 打包格式 cjs/esm/umd
const formats = args.formats || args.f

// 是否使用 sourcemap
const sourceMap = args.sourcemap || args.s
const commit = execa.sync('git', ['rev-parse', 'HEAD']).stdout.slice(0, 7)


// 知道核心是通过 rollup 配置的。
// 需要找到 rollup 的配置文件 rollup.config.js
execa(
  'rollup',
  [
    '-wc',
    '--environment',
    [
      `COMMIT:${commit}`,
      `TARGET:${target}`,
      `FORMATS:${formats || 'global'}`,
      sourceMap ? `SOURCE_MAP:true` : ``
    ]
      .filter(Boolean)
      .join(',')
  ],
  {
    stdio: 'inherit'
  }
)
